/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.controller;

import cz.cvut.fel.taskpro.business.imanager.IAssignmentBusiness;
import cz.cvut.fel.taskpro.business.imanager.ISubjectBusiness;
import cz.cvut.fel.taskpro.business.imanager.IVersionBusiness;
import cz.cvut.fel.taskpro.business.support.BusinessRuleException;
import cz.cvut.fel.taskpro.data.entity.Assignment;
import cz.cvut.fel.taskpro.data.entity.AssignmentSubmit;
import cz.cvut.fel.taskpro.data.entity.GroupTaskPro;
import cz.cvut.fel.taskpro.data.entity.Student;
import cz.cvut.fel.taskpro.data.entity.Subject;
import cz.cvut.fel.taskpro.data.entity.Team;
import cz.cvut.fel.taskpro.data.entity.VersionFile;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 * This controller is responsible for edit assignments. Controller is view scoped
 * and provide operation as load assignemts by kantor, display users who are interest 
 * in and all submitted assignments whit versions.Controller is named as assignModifyBean.
 * @author Tom
 * @version 1.0.0. beta
 */
@ManagedBean(name = "assignModifyBean")
@ViewScoped
public class AssignModifyController extends AbstractFileController implements Serializable {
 
    private List<Subject> kantorsSubjects;
    private List<String> selectedSubject;
    private Map<String, Subject> subjectMap;
    private List<Assignment> kantorsAssignments;
    private Assignment selectedAssignment;
    private List<AssignmentSubmit> assignmentSubmits;
    private AssignmentSubmit selectedAssignmentSubmit;
    private List<VersionFile> assignmentVersionsFile;
    private List<Team> assignmentTeam;
    private List<GroupTaskPro> assignmetGroup;
    private List<Student> assignmentStudent;
    private boolean isGroupTask;
    private boolean isTeamTask;
    private boolean isStudentTask;
    
    
    @EJB
    private IAssignmentBusiness assignmentB;
    @EJB
    private ISubjectBusiness subjectB;
    @EJB
    private IVersionBusiness versionB;
    /**
     * This method initialize class. Recognize user, load all kantor subjects and 
     * add them to filter. Then add all assignment which were assigned by logged kantor.
     */
    @PostConstruct
    public void init() {
        recognizeLogin();
        subjectMap = new HashMap<String, Subject>();
        selectedSubject = new ArrayList<String>();
        try {
            kantorsSubjects = subjectB.getSubjectsByTeacher(loggedKantor);
            Iterator itSub = kantorsSubjects.iterator();
            while (itSub.hasNext()) {
                Subject currSubject = (Subject) itSub.next();
                subjectMap.put(currSubject.getCode(), currSubject);
                selectedSubject.add(currSubject.getCode());
            }
            kantorsAssignments = assignmentB.getAssignmetsByKantorSubject(kantorsSubjects, loggedKantor);
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    /**
     * This method find logged kantor assigned assignments by selected subjects
     */
    public void applyFilter() {
        if (selectedSubject.isEmpty()) {
            kantorsAssignments = null;
            printSuccMessage("0000", "The filter has been setted up.");
            return;
        }
        List<Subject> filteredSubjects = new ArrayList<Subject>();
        Iterator selIterator = selectedSubject.iterator();
        while (selIterator.hasNext()) {
            filteredSubjects.add(subjectMap.get(selIterator.next()));
        }
        try {
            kantorsAssignments = assignmentB.getAssignmetsByKantorSubject(filteredSubjects, loggedKantor);
            printSuccMessage("0000", "The filter has been setted up.");
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    /**
     * This method ask business layer for updating selected assignmet.
     */
    public void updateAssignment() {
        try {
            assignmentB.updateAssignmnet(selectedAssignment);
            printSuccMessage("0000", "The assignment has been updated.");
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    /**
     * This method load all versions file which were submitted in selected submitted assignment
     * submit.
     */
    public void loadVersions() {
        assignmentVersionsFile = versionB.getAllVersionFileByAssingment(selectedAssignmentSubmit);
    }
    /**
     * This method ask business layer for all submitted assignments which were submit
     * in selected assignmets. 
     */
    public void loadAssignmentsSubmit() {
        try {
            assignmentSubmits = assignmentB.getAssignmentSubmitsByAssignment(selectedAssignment);
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    /**
     * This method ask business layer for remove selected assignment. Then
     * removed this assignment from kantor assignment <code> List</code>
     */
    public void removeSelectedAssignment(){
        try {
            assignmentB.removeAssignmen(selectedAssignment);
            kantorsAssignments.remove(selectedAssignment);
            printSuccMessage("Assignmnet remove:","The assignment from " +selectedAssignment.getDateOfAssignment()+" with template " +selectedAssignment.getTemplate().getName()+" has been successfully removed.");
            selectedAssignment=null;
        } catch (BusinessRuleException ex) {
         printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    /**
     * This method ask interest group, team or persons who are member of selected assignment.
     * If assignment is team then load all teams which are on this assignment.
     * If assignment is group then load all group which are on this assignment.
     * If assignment is student then load all student which are on this assignment.
     */
    public void loadAssignmentPersons() {
        if (selectedAssignment.getType() == Assignment.Type.GROUP) {
            isGroupTask = true;
            isStudentTask = false;
            isTeamTask = false;
            assignmetGroup = selectedAssignment.getmGroups();
        } else if (selectedAssignment.getType() == Assignment.Type.TEAM) {
            isGroupTask = false;
            isStudentTask = false;
            isTeamTask = true;
            assignmentTeam = selectedAssignment.getmTeams();
        } else if (selectedAssignment.getType() == Assignment.Type.STUDENT) {
            isGroupTask = false;
            isStudentTask = true;
            isTeamTask = false;
            assignmentStudent = selectedAssignment.getmStudents();
        }
    }

    public List<Assignment> getKantorsAssignments() {
        return kantorsAssignments;
    }

    public void setKantorsAssignments(List<Assignment> kantorsAssignments) {
        this.kantorsAssignments = kantorsAssignments;
    }

    public List<Subject> getKantorsSubjects() {
        return kantorsSubjects;
    }

    public void setKantorsSubjects(List<Subject> kantorsSubjects) {
        this.kantorsSubjects = kantorsSubjects;
    }

    public Assignment getSelectedAssignment() {
        return selectedAssignment;
    }

    public void setSelectedAssignment(Assignment selectedAssignment) {
        this.selectedAssignment = selectedAssignment;
    }

    public List<String> getSelectedSubject() {
        return selectedSubject;
    }

    public void setSelectedSubject(List<String> selectedSubject) {
        this.selectedSubject = selectedSubject;
    }

    public ISubjectBusiness getSubjectB() {
        return subjectB;
    }

    public void setSubjectB(ISubjectBusiness subjectB) {
        this.subjectB = subjectB;
    }

    public Map<String, Subject> getSubjectMap() {
        return subjectMap;
    }

    public void setSubjectMap(Map<String, Subject> subjectMap) {
        this.subjectMap = subjectMap;
    }
    public List<Student> getAssignmentStudent() {
        return assignmentStudent;
    }

    public void setAssignmentStudent(List<Student> assignmentStudent) {
        this.assignmentStudent = assignmentStudent;
    }

    public List<Team> getAssignmentTeam() {
        return assignmentTeam;
    }

    public void setAssignmentTeam(List<Team> assignmentTeam) {
        this.assignmentTeam = assignmentTeam;
    }

    public List<GroupTaskPro> getAssignmetGroup() {
        return assignmetGroup;
    }

    public void setAssignmetGroup(List<GroupTaskPro> assignmetGroup) {
        this.assignmetGroup = assignmetGroup;
    }

    public boolean isIsGroupTask() {
        return isGroupTask;
    }

    public void setIsGroupTask(boolean isGroupTask) {
        this.isGroupTask = isGroupTask;
    }

    public boolean isIsStudentTask() {
        return isStudentTask;
    }

    public void setIsStudentTask(boolean isStudentTask) {
        this.isStudentTask = isStudentTask;
    }

    public boolean isIsTeamTask() {
        return isTeamTask;
    }

    public void setIsTeamTask(boolean isTeamTask) {
        this.isTeamTask = isTeamTask;
    }

    public List<VersionFile> getAssignmentVersionsFile() {
        return assignmentVersionsFile;
    }

    public void setAssignmentVersionsFile(List<VersionFile> assignmentVersionsFile) {
        this.assignmentVersionsFile = assignmentVersionsFile;
    }

    public AssignmentSubmit getSelectedAssignmentSubmit() {
        return selectedAssignmentSubmit;
    }

    public void setSelectedAssignmentSubmit(AssignmentSubmit selectedAssignmentSubmit) {
        this.selectedAssignmentSubmit = selectedAssignmentSubmit;
    }

    public List<AssignmentSubmit> getAssignmentSubmits() {
        return assignmentSubmits;
    }

    public void setAssignmentSubmits(List<AssignmentSubmit> assignmentSubmits) {
        this.assignmentSubmits = assignmentSubmits;
    }
}
    